<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */
defined('IN_IA') or exit('Access Denied');
$_W['page']['title'] = '添加用户 - 用户管理 - 用户管理';
$dos = array('keyword', 'fans', 'transmit', 'post');
$do = in_array($do, $dos) ? $do : 'fans';
if($do == 'keyword') {
	if($_W['isajax']) {
		$condition = '';
		$key_word = trim($_GPC['key_word']);
		if(!empty($key_word)) {
			$condition = " AND content LIKE '%{$key_word}%' AND (module = 'news' OR module = 'cover')";
		} else {
			$condition = " AND (module = 'news' OR module = 'cover')";
		}

		$data = pdo_fetchall('SELECT content, module, rid FROM ' . tablename('rule_keyword') . " WHERE uniacid = :uniacid AND status != 0 " . $condition . ' ORDER BY uniacid DESC,displayorder DESC LIMIT 100', array(':uniacid' => $_W['uniacid']));
		$exit_da = array();
		if(!empty($data)) {
			foreach($data as $da) {
				$exit_da[] = array('content' => $da['content'], 'rid' => $da['rid']);
			}
		}
		exit(json_encode($exit_da));
	}
	exit('error');
}

if($do == 'fans') {
	load()->func('tpl');
	$fanid = intval($_GPC['fanid']);
	$fans = pdo_fetch('SELECT acid,openid FROM ' . tablename('mc_mapping_fans') . ' WHERE uniacid = :uniacid AND fanid = :fanid', array(':uniacid' => $_W['uniacid'], ':fanid' => $fanid));
	template('mc/notice');
	exit();
}

if($do == 'post') {
	$msgtype = trim($_GPC['msgtype']);
	$acid = intval($_GPC['acid']);

	$send['touser'] = trim($_GPC['openid']);
	$send['msgtype'] = $msgtype;
	$fans = pdo_fetch('SELECT salt FROM ' . tablename('mc_mapping_fans') . ' WHERE acid = :acid AND openid = :openid', array(':acid' => $acid, ':openid' => $send['touser']));
	if($msgtype == 'text') {
		$send['text'] = array('content' => urlencode($_GPC['content']));
	} elseif($msgtype == 'image') {
		$send['image'] = array('media_id' => $_GPC['media_id']);
	} elseif($msgtype == 'voice') {
		$send['voice'] = array('media_id' => $_GPC['media_id']);
	} elseif($msgtype == 'video') {
		$send['video'] = array(
			'media_id' => $_GPC['media_id'],
			'thumb_media_id' => $_GPC['thumb_media_id'],
			'title' => urlencode($_GPC['title']),
			'description' => urlencode($_GPC['description'])
		);
	} elseif($msgtype == 'music') {
		$send['music'] = array(
			'musicurl' => $_GPC['musicurl'],
			'hqmusicurl' => $_GPC['hqmusicurl'],
			'title' => urlencode($_GPC['title']),
			'description' => urlencode($_GPC['description']),
			'thumb_media_id' => $_GPC['thumb_media_id'],
		);
	} elseif($msgtype == 'news') {
		$rid = intval($_GPC['ruleid']);
		$module = pdo_fetchcolumn('SELECT module FROM ' . tablename('rule') . ' WHERE id = :rid', array(':rid' => $rid));
		if(empty($module)) {
			exit(json_encode(array('status' => 'error', 'message' => '没有找到指定关键字的回复内容，请检查关键字的对应规则')));
		}
		$reply = pdo_fetchall('SELECT * FROM ' . tablename($module . '_reply') . ' WHERE rid = :rid', array(':rid' => $rid));
		if(!empty($reply)) {
			foreach($reply as $c) {
				$row = array();
				$row['title'] = urlencode($c['title']);
				$row['description'] = urlencode($c['description']);
				!empty($c['thumb']) && $row['picurl'] = tomedia($c['thumb']);

				if(strexists($c['url'], 'http://') || strexists($c['url'], 'https://')) {
					$row['url'] = $c['url'];
				} else {
					$pass['time'] = TIMESTAMP;
					$pass['hash'] = md5("{$fans['openid']}{$pass['time']}{$fans['salt']}{$_W['config']['setting']['authkey']}");
					$auth = base64_encode(json_encode($pass));
					$vars = array();
					$vars['__auth'] = $auth;
					$vars['forward'] = base64_encode($c['url']);
					$row['url'] =  $_W['siteroot'] . 'app/' . murl('auth/forward', $vars);
				}

				$news[] = $row;
			}
			$send['news']['articles'] = $news;
		} else {
			$send['news'] = '';
		}
	}

	if($acid) {
		$acc = WeAccount::create($acid);
		$data = $acc->sendCustomNotice($send);
		if(is_error($data)) {
			exit(json_encode(array('status' => 'error', 'message' => $data['message'])));
		} else {
			exit(json_encode(array('status' => 'success', 'message' => '消息发送成功')));
		}
		exit();
	}

}


